<template>
    <el-card class="box-card">
        <div slot="header" class="clearfix">
            <span>注册</span>
        </div>

        <el-form
            :model="regForm"
            status-icon
            :rules="rules"
            ref="regForm"
            label-width="100px"
        >
            <el-form-item label="用户名" prop="username">
                <el-input v-model="regForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pwd">
                <el-input
                    type="password"
                    v-model="regForm.pwd"
                    autocomplete="off"
                ></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="confirm">
                <el-input
                    type="password"
                    autocomplete="off"
                    v-model="regForm.confirm"
                ></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('regForm')"
                    >提交</el-button
                >
                <el-button @click="resetForm('regForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script>
import { usernameExist } from "../services/user";
export default {
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请输入密码"));
            } else {
                if (this.regForm.confirm !== "") {
                    this.$refs.regForm.validateField("confirm");
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请再次输入密码"));
            } else if (value !== this.regForm.pwd) {
                callback(new Error("两次输入密码不一致!"));
            } else {
                callback();
            }
        };
        return {
            regForm: {
                username: "",
                pwd: "",
                confirm: "",
            },
            rules: {
                username: [
                    {
                        required: true,
                        message: "用户名不能为空",
                        trigger: "blur",
                    },
                    {
                        pattern: /^\w{6,20}$/,
                        message: "格式不正确",
                        trigger: "blur",
                    },
                ],
                pwd: [{ validator: validatePass, trigger: "blur" }],
                confirm: [{ validator: validatePass2, trigger: "blur" }],
            },
        };
    },
    methods: {
        async submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    alert("submit!");
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
    },
};
</script>

<style>
.box-card {
    width: 500px;
    margin: auto;
}
</style>